<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.shop.bean.Seller" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>卖家后台</title>
    <style>
        .container { width: 90%; margin: 0 auto; padding: 20px; }
        .header { display: flex; justify-content: space-between; margin-bottom: 20px; align-items: center; }
        .status-tag { padding: 2px 6px; border-radius: 4px; color: white; font-size: 12px; }
        .tag-online { background-color: #4CAF50; } /* 已上架 */
        .tag-frozen { background-color: #ff9800; } /* 已冻结 */
        .tag-sold { background-color: #9e9e9e; }  /* 已售出 */
        
        table { width: 100%; border-collapse: collapse; margin: 20px 0; }
        th, td { border: 1px solid #ccc; padding: 10px; text-align: left; }
        th { background-color: #f5f5f5; }
        
        .btn { 
            display: inline-block; 
            padding: 6px 12px; 
            margin: 0 4px; 
            text-decoration: none; 
            color: white; 
            border-radius: 4px; 
            font-size: 14px; 
            cursor: pointer; 
        }
        .btn-green { background-color: #4CAF50; }   /* 解冻/选中 */
        .btn-orange { background-color: #ff9800; } /* 冻结 */
        .btn-red { background-color: #f44336; }    /* 标记售出/驳回/取消选中 */
        .btn-blue { background-color: #2196F3; }   /* 其他操作 */
        
        .form-group { margin: 15px 0; }
        input, textarea { 
            width: 300px; 
            padding: 8px; 
            margin-left: 10px; 
            border: 1px solid #ccc; 
            border-radius: 4px; 
        }
        .note { color: #666; font-size: 13px; margin-top: 5px; }
    </style>
</head>
<body>
    <% 
        Seller seller = (Seller) session.getAttribute("seller");
        if (seller == null) {
            response.sendRedirect("seller_login.jsp");
            return;
        }
    %>
    <div class="container">
        <div class="header">
            <h2>欢迎回来，${seller.username}（信誉分：${seller.reputation}）</h2>
            <div>
                <a href="SellerServlet?method=toUpdatePwd" class="btn btn-blue">修改密码</a>
                <a href="SellerServlet?method=logout" class="btn btn-red">退出登录</a>
            </div>
        </div>

        <h3>1. 商品管理</h3>
        <p class="note">说明：冻结商品可见不可买，仅已售出商品隐藏</p>

        <!-- 发布新商品表单 -->
        <c:if test="${empty activeProduct}">
            <form action="SellerServlet?method=addProduct" method="post">
                <div class="form-group">
                    商品名称: <input type="text" name="name" required>
                </div>
                <div class="form-group">
                    商品价格: <input type="number" step="0.01" name="price" required>
                </div>
                <div class="form-group">
                    商品描述: <textarea name="description" required></textarea>
                </div>
                <div class="form-group">
                    图片路径: <input type="text" name="imagePath" placeholder="输入图片URL">
                </div>
                <button type="submit" class="btn btn-green">发布商品</button>
            </form>
        </c:if>

        <!-- 商品列表（冻结商品可见） -->
        <c:if test="${not empty activeProduct}">
            <table>
                <tr>
                    <th>ID</th>
                    <th>名称</th>
                    <th>价格</th>
                    <th>状态</th>
                    <th>游客可见性</th>
                    <th>游客购买权限</th>
                    <th>操作</th>
                </tr>
                <tr>
                    <td>${activeProduct.id}</td>
                    <td>${activeProduct.name}</td>
                    <td>¥${activeProduct.price}</td>
                    <td>
                        <c:choose>
                            <c:when test="${activeProduct.status == 1}">
                                <span class="status-tag tag-online">已上架</span>
                            </c:when>
                            <c:when test="${activeProduct.status == 2}">
                                <span class="status-tag tag-frozen">已冻结</span>
                            </c:when>
                            <c:when test="${activeProduct.status == 3}">
                                <span class="status-tag tag-sold">已售出</span>
                            </c:when>
                        </c:choose>
                    </td>
                    <td>
                        <c:choose>
                            <c:when test="${activeProduct.status == 3}">
                                <span style="color: red; font-weight: bold;">× 隐藏</span>
                            </c:when>
                            <c:otherwise>
                                <span style="color: green; font-weight: bold;">√ 可见</span>
                            </c:otherwise>
                        </c:choose>
                    </td>
                    <td>
                        <c:choose>
                            <c:when test="${activeProduct.status == 1}">
                                <span style="color: green; font-weight: bold;">√ 可购买</span>
                            </c:when>
                            <c:otherwise>
                                <span style="color: red; font-weight: bold;">× 不可购买</span>
                            </c:otherwise>
                        </c:choose>
                    </td>
                    <td>
                        <!-- 状态联动操作按钮 -->
                        <c:choose>
                            <c:when test="${activeProduct.status == 1}">
                                <a href="SellerServlet?method=updateProductStatus&productId=${activeProduct.id}&status=2" 
                                   class="btn btn-orange">冻结商品</a>
                                <a href="SellerServlet?method=updateProductStatus&productId=${activeProduct.id}&status=3" 
                                   class="btn btn-red">标记售出</a>
                            </c:when>
                            <c:when test="${activeProduct.status == 2}">
                                <a href="SellerServlet?method=updateProductStatus&productId=${activeProduct.id}&status=1" 
                                   class="btn btn-green">解冻商品</a>
                                <a href="SellerServlet?method=updateProductStatus&productId=${activeProduct.id}&status=3" 
                                   class="btn btn-red">标记售出</a>
                            </c:when>
                            <c:when test="${activeProduct.status == 3}">
                                <span>已完成交易</span>
                            </c:when>
                        </c:choose>
                    </td>
                </tr>
            </table>
        </c:if>

        <!-- 购买意向列表（添加取消选中按钮） -->
        <h3>2. 购买意向列表</h3>
        <table>
            <tr>
                <th>ID</th>
                <th>买家姓名</th>
                <th>交易地点</th>
                <th>交易时间</th>
                <th>联系电话</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            <c:forEach items="${intents}" var="intent">
                <tr>
                    <td>${intent.id}</td>
                    <td>${intent.buyerName}</td>
                    <td>${intent.location}</td>
                    <td>${intent.tradeTime}</td>
                    <td>${intent.phone}</td>
                    <td>
                        <c:choose>
                            <c:when test="${intent.status == 1}">待确认</c:when>
                            <c:when test="${intent.status == 2}">已选中</c:when>
                            <c:when test="${intent.status == 3}">已驳回</c:when>
                            <c:when test="${intent.status == 5}">已取消</c:when>
                        </c:choose>
                    </td>
                    <td>
                        <c:choose>
                            <c:when test="${intent.status == 1}">
                                <a href="SellerServlet?method=handleIntent&intentId=${intent.id}&productId=${activeProduct.id}&status=2" 
                                   class="btn btn-green">选中</a>
                                <a href="SellerServlet?method=handleIntent&intentId=${intent.id}&productId=${activeProduct.id}&status=3&reason=时间不合适" 
                                   class="btn btn-red">驳回</a>
                            </c:when>
                            <c:when test="${intent.status == 2}">
                                <!-- 已选中状态显示取消选中按钮 -->
                                <a href="SellerServlet?method=cancelSelected&intentId=${intent.id}" 
                                   class="btn btn-red">取消选中</a>
                            </c:when>
                        </c:choose>
                    </td>
                </tr>
            </c:forEach>
        </table>

        <a href="SellerServlet?method=showHistory" class="btn btn-blue">查看历史记录</a>
    </div>
</body>
</html>